package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.*;
import cn.com.api.service.ApiMemberService;
import cn.com.api.service.ApiOrderService;
import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiT_member_entrepreneurService;
import cn.com.api.service.ApiT_member_idcardService;
import cn.com.api.service.ApiT_member_partnerService;
import cn.com.api.service.ApiT_member_wemediaService;
import cn.com.api.service.ApiT_shipper_accountService;
import cn.com.api.service.ApiT_shippingService;
import cn.com.api.service.ApiT_statsService;
import cn.com.api.service.StartOnLoadService;
import cn.com.backstage.controller.base.T_memberBaseController;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE + "/")
public class T_memberController extends T_memberBaseController {

	private static Logger logger = Logger.getLogger(T_memberController.class);

	@Autowired
	private ApiMemberService apiMemberService;
	@Autowired
	private ApiT_memberService apiT_memberService;
	@Autowired
	private StartOnLoadService startOnLoadService;
	@Autowired
	private ApiT_shipper_accountService apiT_shipper_accountService;
	@Autowired
	private ApiT_shippingService apiT_shippingService;
	@Autowired
	private ApiT_statsService apiT_statsService;
	@Autowired
	private ApiOrderService apiOrderService;
	@Autowired
	private ApiT_member_idcardService apiT_member_idcardService;
	@Autowired
	private ApiT_member_partnerService apiT_member_partnerService;
	@Autowired
	private ApiT_member_entrepreneurService apiT_member_entrepreneurService;
	@Autowired
	private ApiT_member_wemediaService apiT_member_wemediaService;

	/**
	 * 查询
	 * 
	 * @return
	 */
	@RequestMapping(value = "member-search")
	public ModelAndView member_search(HttpServletRequest request, HttpServletResponse response, PageInfo pageInfo)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_member_list.jsp");

		Map<String, Object> conditions = new HashMap<String, Object>();
		String c_tname = request.getParameter("c_tname");
		String c_nick = request.getParameter("c_nick");
		String c_mob = request.getParameter("c_mob");
		String c_city = request.getParameter("c_city");
		conditions.put("c_tname", c_tname);
		conditions.put("c_nick", c_nick);
		conditions.put("c_mob", c_mob);
		conditions.put("c_city", c_city);

		List<ApiT_member> t_members = apiT_memberService.searchMember(conditions, pageInfo);
		mv.addObject("t_members", t_members);

		for (ApiT_member member : t_members) {
			try {
				member.setStr_regdate(member.getC_regdate());
			} catch (Exception e) {
			}
		}
		mv.addObject("t_members", t_members);
		//
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		//
		return mv;
	}

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "member_list")
	public ModelAndView member_list(HttpServletRequest request, HttpServletResponse response, PageInfo pageInfo)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_member_list.jsp");
		Map<String, Object> conditions = new HashMap<String, Object>();
		List<ApiT_member> t_members = apiT_memberService.searchMember(conditions, pageInfo);
		for (ApiT_member member : t_members) {
			member.setStr_regdate(member.getC_regdate());
		}
		mv.addObject("t_members", t_members);

		// List<ApiT_city> citys = startOnLoadService.citys();
		// mv.addObject("citys", citys);

		return mv;
	}

	/**
	 * 身份认证用户列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "member_ids-list")
	public ModelAndView member_ids_list(HttpServletRequest request, HttpServletResponse response, PageInfo pageInfo)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_member_idcard_list.jsp");

		Map<String, Object> conditions = new HashMap<String, Object>();
		List<Map<String, Object>> t_members = apiT_member_idcardService.findMemberinfosByMap(conditions);

		mv.addObject("t_member_idcards", t_members);
		//
		return mv;
	}

	/**
	 * 进入编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "member_show")
	public ModelAndView member_show(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_member_show.jsp");
		String memberid = request.getParameter("memberid");
		ApiT_member apiT_member = apiT_memberService.findT_memberById(Integer.valueOf(memberid));
		memberHomePage homePage = this.apiMemberService.getUserHomePageInfo(memberid, 3, memberid);
		mv.addObject("homePage", homePage);
		mv.addObject("apiT_member", apiT_member);
		return mv;
	}

	/**
	 * 进入编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "update_member_money")
	public void update_member_money(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String money = request.getParameter("money");
		String user_id = request.getParameter("user_id");
		ApiT_member apiT_member = apiT_memberService.findT_memberById(Integer.valueOf(user_id));
		apiT_member.setC_money(new BigDecimal(money));
		apiT_memberService.updateT_member(apiT_member);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "修改成功！");
		JsonUtil.printJson(response, JsonUtil.object2json(map));
	}

	/**
	 * 进入编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "member-add-pre")
	public ModelAndView member_add_pre(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_member_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_member t_member = apiT_memberService.findT_memberById(Integer.valueOf(id));
			// reg date
			try {
				t_member.setStr_regdate(t_member.getC_regdate());
			} catch (Exception e) {

			}
			mv.addObject("t_member", t_member);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "member-add")
	public void member_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String c_pass = request.getParameter("c_pass");
		String c_wt = request.getParameter("c_wt");
		String c_da = request.getParameter("c_da");
		String c_nick = request.getParameter("c_nick");
		String c_tname = request.getParameter("c_tname");
		String c_city = request.getParameter("c_city");
		String c_area = request.getParameter("c_area");
		String c_quan = request.getParameter("c_quan");
		String c_mob = request.getParameter("c_mob");
		String c_mail = request.getParameter("c_mail");
		String c_qq = request.getParameter("c_qq");
		String c_sex = request.getParameter("c_sex");
		String c_jf = request.getParameter("c_jf");
		String c_avatar = request.getParameter("c_avatar");
		String c_cname = request.getParameter("c_cname");
		String c_faren = request.getParameter("c_faren");
		String c_license = request.getParameter("c_license");
		String c_pname = request.getParameter("c_pname");
		String c_address = request.getParameter("c_address");
		String c_zip = request.getParameter("c_zip");
		String c_tel = request.getParameter("c_tel");
		String c_money = request.getParameter("c_money");
		String c_thirdtoken = request.getParameter("c_thirdtoken");
		String deviceid = request.getParameter("deviceid");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_member t_member = new ApiT_member();

			// t_member.setC_id(c_id); t_member.setC_userid(c_userid);
			t_member.setC_pass(c_pass);
			t_member.setC_wt(c_wt);
			t_member.setC_da(c_da);
			t_member.setC_nick(c_nick);
			t_member.setC_tname(c_tname);
			t_member.setC_city(c_city);
			t_member.setC_area(c_area);
			t_member.setC_quan(c_quan);
			t_member.setC_mob(c_mob);
			t_member.setC_mail(c_mail);
			t_member.setC_qq(c_qq);
			t_member.setC_sex(c_sex);
			// t_member.setC_ip(c_ip); t_member.setC_regdate(c_regdate);
			// t_member.setC_logdate(c_logdate); t_member.setC_jf(c_jf);
			t_member.setC_avatar(c_avatar);
			t_member.setC_cname(c_cname);
			t_member.setC_faren(c_faren);
			t_member.setC_license(c_license);
			t_member.setC_pname(c_pname);
			t_member.setC_address(c_address);
			t_member.setC_zip(c_zip);
			t_member.setC_tel(c_tel);
			// t_member.setC_fax(c_fax); t_member.setC_mtype(c_mtype);
			// t_member.setC_invite(c_invite); t_member.setT_card(t_card);
			// t_member.setC_money(c_money); t_member.setToken(token);
			// t_member.setC_age(c_age); t_member.setC_thirdtype(c_thirdtype);
			t_member.setC_thirdtoken(c_thirdtoken);
			// t_member.setOpenid(openid); t_member.setDevicetype(devicetype);
			t_member.setDeviceid(deviceid);

			// apiT_memberService.insertT_member(t_member);
		}
		// edit
		else {
			ApiT_member t_member = apiT_memberService.findT_memberById(Integer.valueOf(id));
			/*
			 * t_member.setC_id(c_id); t_member.setC_userid(c_userid);
			 * t_member.setC_pass(c_pass); t_member.setC_wt(c_wt);
			 * t_member.setC_da(c_da); t_member.setC_nick(c_nick);
			 * t_member.setC_tname(c_tname); t_member.setC_city(c_city);
			 * t_member.setC_area(c_area); t_member.setC_quan(c_quan);
			 * t_member.setC_mob(c_mob); t_member.setC_mail(c_mail);
			 * t_member.setC_qq(c_qq); t_member.setC_sex(c_sex);
			 * t_member.setC_ip(c_ip); t_member.setC_regdate(c_regdate);
			 * t_member.setC_logdate(c_logdate); t_member.setC_jf(c_jf);
			 * t_member.setC_avatar(c_avatar); t_member.setC_cname(c_cname);
			 * t_member.setC_faren(c_faren); t_member.setC_license(c_license);
			 * t_member.setC_pname(c_pname); t_member.setC_address(c_address);
			 * t_member.setC_zip(c_zip); t_member.setC_tel(c_tel);
			 * t_member.setC_fax(c_fax); t_member.setC_mtype(c_mtype);
			 * t_member.setC_invite(c_invite); t_member.setT_card(t_card);
			 */
			t_member.setC_money(new BigDecimal(c_money));
			t_member.setC_jf(Integer.valueOf(c_jf));
			// t_member.setToken(token);
			// t_member.setC_age(c_age); t_member.setC_thirdtype(c_thirdtype);
			// t_member.setC_thirdtoken(c_thirdtoken);
			// t_member.setOpenid(openid); t_member.setDevicetype(devicetype);
			// t_member.setDeviceid(deviceid);
			apiT_memberService.updateT_member(t_member);
			//
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "修改成功！");
		}
		//
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_shipper-list")
	public ModelAndView t_shipper_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_member_list.jsp");
		// 0用户 1 绑定商户 2 vip 9被屏蔽 3 配送员
		List<ApiT_member> t_members = apiT_memberService.findT_membersByType(3);
		mv.addObject("t_members", t_members);

		return mv;
	}

	// 配送员列表
	@RequestMapping(value = "t_member_shipper_list")
	public ModelAndView t_member_shipper_list(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_member_shipper_list.jsp");
		List<Member_shipper> t_member_shipper = apiT_shipper_accountService.m_shipperlist(pageInfo);
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		mv.addObject("working_status", -1);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("t_member_shipper", t_member_shipper);
		// 查询配送中的订单
		List<ApiT_shipping> shippings = apiT_shippingService.shipping_ing();
		mv.addObject("shippings", shippings);
		return mv;
	}

	// 配送员结算报表(单日)
	@RequestMapping("shipper_statistic_list")
	public ModelAndView shipperStatisticList(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo, ApiT_stats conditions) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/shipper_statistic_list.jsp");
		List<ApiT_stats> stats = apiT_statsService.shipperStatList(conditions, pageInfo);
		mv.addObject("stats", stats);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);

		return mv;
	}

	// 结算或者充值 type -- 1: 充值 ; 2:结算
	@RequestMapping("update_stat_status")
	public @ResponseBody Map<String, String> updateStatStatus(int stats_id, int type, double money) {
		Map<String, String> result = new HashMap<String, String>();
		try {
			if (type == 1) {
				result = apiT_statsService.updateStatStatus(stats_id, 1, money);
			}
			if (type == 2) {
				result = apiT_statsService.updateStatStatus(stats_id, 2, money);
			}
		} catch (Exception ex) {
			result.put("error", "-1");
		}
		return result;
	}

	// 结算或者充值 type -- 1: 充值 ; 2:结算
	@RequestMapping("update_station_stat_status")
	public @ResponseBody Map<String, String> updateStationStatStatus(int stats_id, int type, double money) {
		Map<String, String> result = new HashMap<String, String>();
		try {
			if (type == 1) {
				result = apiT_statsService.updateStationStatStatus(stats_id, 1, money);
			}
			if (type == 2) {
				result = apiT_statsService.updateStationStatStatus(stats_id, 2, money);
			}
		} catch (Exception ex) {
			result.put("error", "-1");
		}
		return result;
	}

	// 配送员结算报表(日报)
	@RequestMapping("shipper_month_statistic_list")
	public ModelAndView shipperMonthStatisticList(HttpServletRequest request, PageInfo pageInfo, ApiT_stats conditions)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/shipper_month_statistic_list.jsp");

		List<ApiT_stats> stats = apiT_statsService.shipperMonthStatList(conditions, pageInfo);
		mv.addObject("stats", stats);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		return mv;
	}

	// 配送员结算账单(按月)
	@RequestMapping("shipper_month_billing_list")
	public ModelAndView shipperMonthBillingList(HttpServletRequest request, PageInfo pageInfo, ApiT_stats conditions)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/shipper_month_billing_list.jsp");
		List<ApiT_stats> stats = apiT_statsService.shipperMonthBillingList(conditions, pageInfo);
		mv.addObject("stats", stats);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		return mv;
	}

	// 审核配送配送员帐单
	@RequestMapping("updateShipperBillingStatus")
	public @ResponseBody Map<String, String> updateShipperBillingStatus(int stats_id, int status, String remark) {
		Map<String, String> result = new HashMap<String, String>();
		try {
			boolean updateResult = false;
			if (status == 1) {
				updateResult = apiT_statsService.updateShipperMonthBillingStatusAndRemark(stats_id, 1, remark);
			}
			if (status == 2) {
				updateResult = apiT_statsService.updateShipperMonthBillingStatusAndRemark(stats_id, 2, remark);
			}
			if (updateResult) {
				result.put("error", "0000");// 更新成功
			}
		} catch (Exception ex) {
			result.put("error", "-1");
		}
		return result;
	}

	// 确认打款给配送员
	@RequestMapping("payToShipper")
	public @ResponseBody Map<String, String> payToShipper(int stats_id, ApiT_member_pay_records record) {
		Map<String, String> result = new HashMap<String, String>();
		try {
			boolean updateResult = apiT_statsService.payToShipper(stats_id, record);
			if (updateResult) {
				result.put("error", "0000");// 更新成功
			}
		} catch (Exception ex) {
			result.put("error", "-1");
		}
		return result;
	}

	// 配送员配送的具体订单
	@RequestMapping("shipper_order_list")
	public ModelAndView shipperOrderList(HttpServletRequest request, PageInfo pageInfo, String userId, Integer year,
			Integer month, Integer day, Integer stats_id, String finish_time) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/shipper_order_list.jsp");

		String zftype = request.getParameter("zftype");
		String title = request.getParameter("title");
		String name = request.getParameter("name");
		String stime = request.getParameter("stime");
		String etime = request.getParameter("etime");

		Map<String, Object> conditions = new HashMap<String, Object>();

		conditions.put("title", title != null ? title.trim() : title);
		conditions.put("zftype", zftype != null ? zftype.trim() : zftype);
		conditions.put("name", name != null ? name.trim() : name);
		conditions.put("stime", stime);
		conditions.put("etime", etime);

		conditions.put("userId", userId);
		conditions.put("year", year);
		conditions.put("month", month);
		conditions.put("finish_time", finish_time);
		conditions.put("day", day);
		List<Map<String, Object>> orders = apiOrderService.searchOrder(conditions, pageInfo);
		if (stats_id != null) {
			ApiT_stats stat = apiT_statsService.getStatById(stats_id);
			mv.addObject("stat", stat);
		}

		mv.addObject("orders", orders);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		return mv;
	}

	// 商家具体订单(按月)
	@RequestMapping("shop_shipper_order_list")
	public ModelAndView shopShipperOrderList(HttpServletRequest request, PageInfo pageInfo, Integer shop_id,
			Integer year, Integer month) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/shop_shipper_order_list.jsp");

		String zftype = request.getParameter("zftype");
		String title = request.getParameter("title");
		String name = request.getParameter("name");
		String stime = request.getParameter("stime");
		String etime = request.getParameter("etime");

		Map<String, Object> conditions = new HashMap<String, Object>();

		conditions.put("title", title != null ? title.trim() : title);
		conditions.put("zftype", zftype != null ? zftype.trim() : zftype);
		conditions.put("name", name != null ? name.trim() : name);
		conditions.put("stime", stime);
		conditions.put("etime", etime);

		conditions.put("year", year);
		conditions.put("month", month);
		conditions.put("shop_id", shop_id);
		List<Map<String, Object>> orders = apiOrderService.searchOrder(conditions, pageInfo);

		mv.addObject("orders", orders);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		return mv;
	}

	// 商家具体订单(按周)
	@RequestMapping("shop_shipper_week_order_list")
	public ModelAndView shopShipperWeekOrderList(HttpServletRequest request, PageInfo pageInfo, Integer shop_id,
			String startTime, String endTime) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/shop_shipper_week_order_list.jsp");

		String zftype = request.getParameter("zftype");
		String title = request.getParameter("title");
		String name = request.getParameter("name");
		String stime = request.getParameter("stime");
		String etime = request.getParameter("etime");

		Map<String, Object> conditions = new HashMap<String, Object>();

		conditions.put("title", title != null ? title.trim() : title);
		conditions.put("zftype", zftype != null ? zftype.trim() : zftype);
		conditions.put("name", name != null ? name.trim() : name);
		conditions.put("stime", stime);
		conditions.put("etime", etime);

		conditions.put("startTime", startTime);
		conditions.put("endTime", endTime);
		conditions.put("shop_id", shop_id);
		List<Map<String, Object>> orders = apiOrderService.searchOrder(conditions, pageInfo);

		mv.addObject("orders", orders);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		return mv;
	}

	// 条件查询配送员列表
	@RequestMapping(value = "shipper_list_byterm")
	public ModelAndView shipper_list_byterm(HttpServletRequest request, HttpServletResponse response, PageInfo pageInfo)
			throws Exception {
		Map<String, Object> conditions = new HashMap<String, Object>();
		String working_status = request.getParameter("working_status");
		String c_tname = request.getParameter("c_tname");
		String c_mob = request.getParameter("c_mob");
		String c_city = request.getParameter("c_city");
		String id = request.getParameter("id");
		request.setAttribute("id", id);
		request.setAttribute("working_status", working_status);
		request.setAttribute("c_tname", c_tname);
		request.setAttribute("c_mob", c_mob);
		request.setAttribute("c_city", c_city);
		conditions.put("working_status", working_status);
		conditions.put("c_tname", c_tname);
		conditions.put("c_mob", c_mob);
		conditions.put("c_city", c_city);
		conditions.put("id", id);
		ModelAndView mv = new ModelAndView("forward:/backstage/t_member_shipper_list.jsp");
		List<Member_shipper> t_member_shipper = apiT_shipper_accountService.shipper_list_bytermListPage(conditions,
				pageInfo);
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		mv.addObject("t_member_shipper", t_member_shipper);

		return mv;
	}

	/**
	 * 待转账的配送员账单列表
	 * 
	 * @param conditions
	 *            不同搜索条件
	 * @param pageInfo
	 *            分页信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("shipper-cash-transfer-list")
	public ModelAndView cashTransferList(ApiT_stats conditions, PageInfo pageInfo) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/shipper-cash-transfer-list.jsp");
		List<ApiT_stats> stats = apiT_statsService.checkedStatsList(conditions, pageInfo);

		mv.addObject("stats", stats);
		mv.addObject("conditions", conditions);

		return mv;
	}

	// 根据手机号查询配送员
	@RequestMapping(value = "shipperbymob")
	public void shipperbymob(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String c_mob = "D" + request.getParameter("c_mob");
		ApiT_member member = apiT_memberService.findT_membersByUserid(c_mob);
		int resultid = 0;
		if (member != null) {
			resultid = 1;
		}
		map.put("resultid", resultid);
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 配送员进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_member_shipper-add-pre")
	public ModelAndView t_member_shipper_add_pre(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_member_shipper_add.jsp");
		//
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_shipper_account shipper_account = apiT_shipper_accountService
					.findT_shipper_accountById(Integer.parseInt(id));
			ApiT_member member = apiT_memberService.findT_membersByUserid(shipper_account.getUser_id());
			member.setBank_card(shipper_account.getBank_card());
			member.setBank_num(shipper_account.getBank_num());
			member.setID_card(shipper_account.getID_card());
			member.setHealth_card(shipper_account.getHealth_card());
			member.setID_num(shipper_account.getID_num());
			member.setReserve_money(shipper_account.getReserve_money());
			member.setBasic_wage(shipper_account.getBasic_wage());
			mv.addObject("t_member", member);
			mv.addObject("shipper_account", shipper_account);

		}

		response.setCharacterEncoding("UTF-8");
		return mv;
	}

	/**
	 * 配送员新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_member_shipper-add")
	public void t_member_shipper_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String c_pass = request.getParameter("c_pass");
		c_pass = CipherUtil.generatePassword(c_pass);
		String c_tname = request.getParameter("c_tname");
		String c_mob = request.getParameter("c_mob");
		String c_address = request.getParameter("c_address");
		String c_city = request.getParameter("c_city");
		String bank_card = request.getParameter("bank_card");
		String bank_num = request.getParameter("bank_num");
		String ID_card = request.getParameter("ID_card");
		String health_card = request.getParameter("health_card");
		String ID_num = request.getParameter("ID_num");
		String c_pname = request.getParameter("c_pname");
		String reserve_money = request.getParameter("reserve_money");
		// String c_avatar = request.getParameter("c_avatar");
		// 0用户 1 绑定商户 2 vip 9被屏蔽 3 配送员
		// String c_mtype = request.getParameter("c_mtype");

		String c_money = request.getParameter("c_money");
		String basic_wage = request.getParameter("basic_wage");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_member t_member = new ApiT_member();
			t_member.setReserve_money(Util.getBigDecimal(reserve_money));
			t_member.setC_userid("D" + c_mob);
			t_member.setC_pass(c_pass);
			t_member.setC_tname(c_tname);
			t_member.setC_mob(c_mob);
			// 0用户 1 绑定商户 2 vip 9被屏蔽 3 配送员
			t_member.setC_mtype(3);
			t_member.setC_money(new BigDecimal(c_money));
			t_member.setC_address(c_address);
			t_member.setC_city(c_city);
			t_member.setC_pname(c_pname);
			t_member.setC_wt("");
			t_member.setC_da("");
			t_member.setC_nick("");
			t_member.setC_area(null);
			t_member.setC_quan(null);
			t_member.setC_mail(null);
			t_member.setC_qq(null);
			t_member.setC_sex(null);
			t_member.setC_ip(null);
			t_member.setC_regdate(null);
			t_member.setC_logdate(null);
			t_member.setC_jf(null);
			t_member.setC_avatar(null);
			t_member.setC_cname(null);
			t_member.setC_faren(null);
			t_member.setC_license(null);

			t_member.setC_zip(null);
			t_member.setC_tel(null);
			t_member.setC_fax(null);

			t_member.setC_invite(null);
			t_member.setT_card(null);
			t_member.setToken(null);
			t_member.setC_age(null);
			t_member.setC_thirdtype(null);
			t_member.setC_thirdtoken(null);
			t_member.setOpenid(null);
			t_member.setDevicetype(null);
			t_member.setDeviceid(null);
			t_member.setBank_card(bank_card);
			t_member.setBank_num(bank_num);
			t_member.setID_card(ID_card);
			t_member.setHealth_card(health_card);
			t_member.setID_num(ID_num);
			t_member.setBasic_wage(Double.valueOf(basic_wage));
			// 新增配送员
			apiT_memberService.insertT_memberShipper(t_member);
		}
		// edit
		else {
			ApiT_member t_member = apiT_memberService.findT_memberById(Integer.valueOf(id));
			t_member.setReserve_money(Util.getBigDecimal(reserve_money));
			t_member.setC_userid("D" + c_mob);
			t_member.setC_pass(c_pass);
			t_member.setC_city(c_city);
			t_member.setC_tname(c_tname);
			t_member.setC_mob(c_mob);
			t_member.setC_pname(c_pname);
			t_member.setC_address(c_address);
			t_member.setC_money(new BigDecimal(c_money));
			t_member.setBank_card(bank_card);
			t_member.setBank_num(bank_num);
			t_member.setID_card(ID_card);
			t_member.setHealth_card(health_card);
			t_member.setID_num(ID_num);
			t_member.setBasic_wage(Double.valueOf(basic_wage));
			apiT_memberService.editorT_memberShipper(t_member);
		}
		//

		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 删除配送员
	@RequestMapping(value = "t_member_shipper_del")
	public void t_member_shipper_del(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		ApiT_shipper_account shipper_account = apiT_shipper_accountService
				.findT_shipper_accountById(Integer.parseInt(id));
		ApiT_member member = apiT_memberService.findT_membersByUserid(shipper_account.getUser_id());
		apiT_shipper_accountService.deleteT_shipper_account(shipper_account.getId());
		// delete
		int ret_i = apiT_memberService.deleteT_member(Integer.valueOf(member.getC_id()));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	// 配送员详情页面
	@RequestMapping(value = "t_member_shipper-xq")
	public ModelAndView t_member_shipper_xq(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_member_shipper_xq.jsp");
		//
		List<ApiT_city> citys = startOnLoadService.citys();
		mv.addObject("citys", citys);
		String id = request.getParameter("id");
		ApiT_shipper_account shipper_account = apiT_shipper_accountService
				.findT_shipper_accountById(Integer.parseInt(id));
		ApiT_member member = apiT_memberService.findT_membersByUserid(shipper_account.getUser_id());
		member.setBank_card(shipper_account.getBank_card());
		member.setBank_num(shipper_account.getBank_num());
		member.setID_card(shipper_account.getID_card());
		member.setHealth_card(shipper_account.getHealth_card());
		member.setID_num(shipper_account.getID_num());
		mv.addObject("t_member", member);
		mv.addObject("shipperAccount", shipper_account);
		response.setCharacterEncoding("UTF-8");
		return mv;
	}

	// 配送员结算列表
	@RequestMapping(value = "t_shipper_cash_list")
	public ModelAndView t_shipper_cash_list(HttpServletRequest request, HttpServletResponse response, PageInfo pageInfo)
			throws Exception {
		String status = request.getParameter("status");
		String date1 = request.getParameter("date1");
		String date2 = request.getParameter("date2");
		ModelAndView mv = new ModelAndView("forward:/backstage/t_shipper_cash_list.jsp");
		List<Finance_balance> shippers = apiT_memberService.shipper_cash_list(pageInfo, status, date1, date2);
		request.setAttribute("status", status);
		request.setAttribute("date1", date1);
		request.setAttribute("date2", date2);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("shippers", shippers);

		return mv;
	}

	/** 用户充值信息 */
	/**
	 * 用户充值汇总 条件查询 手机号，用户名
	 */
	@RequestMapping(value = "t_member_charge_list")
	public ModelAndView t_member_charge_list(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception {
		String u_id = request.getParameter("u_id");
		String u_name = request.getParameter("u_name");

		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("u_id", u_id);
		conditions.put("u_name", u_name);

		ModelAndView mv = new ModelAndView("forward:/backstage/t_member_charge_list.jsp");
		List<Member_Charge_List> memberList = new ArrayList<Member_Charge_List>();
		memberList = apiT_memberService.searchMemberChargeList(pageInfo, conditions);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("memberList", memberList);

		return mv;
	}

	@RequestMapping(value = "t_member_charge_detail_list")
	public ModelAndView t_member_charge_detail_list(HttpServletRequest request, HttpServletResponse response,
			PageInfo pageInfo) throws Exception {
		String u_id = request.getParameter("u_id");
		String u_name = request.getParameter("u_name");

		Map<String, Object> conditions = new HashMap<String, Object>();
		conditions.put("u_id", u_id);
		conditions.put("u_name", u_name);

		ModelAndView mv = new ModelAndView("forward:/backstage/t_member_charge_detail_list.jsp");
		List<Member_Charge_List> memberList = new ArrayList<Member_Charge_List>();
		memberList = apiT_memberService.searchMemberChargeDetailList(pageInfo, conditions);
		mv.addObject("conditions", conditions);
		mv.addObject("pageInfo", pageInfo);
		mv.addObject("memberList", memberList);

		return mv;
	}

	/**
	 * 跳转用户实名认证信息界面
	 */
	@RequestMapping(value = "member_idcard_detail")
	public ModelAndView member_idcard_detail(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/member_idcard_detail.jsp");
		String userid = request.getParameter("userid");
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", userid);
		List<ApiT_member_idcard> list = apiT_member_idcardService.findT_member_idcardByMap(map);
		mv.addObject("idcarddetail", list.get(0));
		return mv;
	}

	/**
	 * 后台审核实名认证
	 */
	@RequestMapping(value = "validate_member_idcard")
	public void validate_member_idcard(HttpServletRequest request, HttpServletResponse response) throws Exception {
		BaseObj baseObj = new BaseObj();
		try {
			ApiT_user_admin user = (ApiT_user_admin) request.getSession().getAttribute("user_admin");
			String id = request.getParameter("id");
			String reason = request.getParameter("reason");
			String status = request.getParameter("status");

			// 跟新申请资料表
			ApiT_member_idcard apiT_member_idcard = apiT_member_idcardService
					.findT_member_idcardById(Integer.valueOf(id));
			apiT_member_idcard.setApply_uid(user.getUser_id());
			apiT_member_idcard.setUpdate_time(DateUtil.nowTimeToString());
			apiT_member_idcard.setReason(reason);
			apiT_member_idcard.setStatus(Integer.valueOf(status));
			apiT_member_idcardService.updateT_member_idcard(apiT_member_idcard);

			// 跟新用户表状态
			ApiT_member apiT_member = apiT_memberService.findT_memberById(apiT_member_idcard.getUser_id());
			if ("1".equals(status)) {
				apiT_member.setVerify_idcard(1);
			} else {
				apiT_member.setVerify_idcard(3);
			}
			apiT_member.setC_tname(apiT_member_idcard.getName());

			apiT_memberService.updateT_member(apiT_member);
			baseObj.handleSuccess();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			baseObj.handleException(e);
		}
		JsonUtil.printJson(response, JsonUtil.object2json(baseObj));
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "member-delete")
	public void member_delete(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String id = request.getParameter("id");

		BaseObj baseObj = new BaseObj();
		try {
			// delete
			List<Map<String, Object>> result_list = null;
			Set<String> keys = null;
			Object the_id = null;
			Map<String, Object> condition = new HashMap<String, Object>();

			try {
				// 根据user_id查询身份认证记录
				condition.put("user_id", id);
				result_list = apiT_member_idcardService.findMemberinfosByMap(condition);
				Map<String, Object> idcard = result_list.get(0);
				keys = idcard.keySet();
				for (String s : idcard.keySet()) { // 取map集合里的String类型的key，
					if (s.equalsIgnoreCase("ID")) {
						the_id = idcard.get(s); // 根据key迭代输出value
					}
				}
				apiT_member_idcardService.deleteT_member_idcard(Integer.parseInt(the_id.toString()));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				baseObj.handleException(e);
			}

			// 根据user_id查询身份认证记录
			try {
				result_list = apiT_member_partnerService.findMember_partnersByMap(condition);
				Map<String, Object> partner = result_list.get(0);

				keys = partner.keySet();
				for (String s : partner.keySet()) { // 取map集合里的String类型的key，
					if (s.equalsIgnoreCase("ID")) {
						the_id = partner.get(s); // 根据key迭代输出value
					}
				}
				apiT_member_partnerService.deleteT_member_partner(Integer.parseInt(the_id.toString()));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				baseObj.handleException(e);
			}
			// 根据user_id查询企业主记录
			try {
				result_list = apiT_member_entrepreneurService.findMember_entrepreneursByMap(condition);
				Map<String, Object> corp = result_list.get(0);
				keys = corp.keySet();
				for (String s : corp.keySet()) { // 取map集合里的String类型的key，
					if (s.equalsIgnoreCase("ID")) {
						the_id = corp.get(s); // 根据key迭代输出value
					}
				}
				apiT_member_entrepreneurService.deleteT_member_entrepreneur(Integer.parseInt(the_id.toString()));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				baseObj.handleException(e);
			}
			// 根据user_id查询企业主记录
			try {
				result_list = apiT_member_wemediaService.findMember_wemediasByMap(condition);
				Map<String, Object> media = result_list.get(0);
				keys = media.keySet();
				for (String s : media.keySet()) { // 取map集合里的String类型的key，
					if (s.equalsIgnoreCase("ID")) {
						the_id = media.get(s); // 根据key迭代输出value
					}
				}
				apiT_member_wemediaService.deleteT_member_wemedia(Integer.parseInt(the_id.toString()));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				baseObj.handleException(e);
			}
			// 会员基本信息
			int ret_i = apiT_memberService.deleteT_member(Integer.valueOf(id));

			//
			if (ret_i > 0) {
				baseObj.handleSuccess();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			baseObj.handleException(e);
		}

		JsonUtil.printJson(response, JsonUtil.object2json(baseObj));
	}

}
